package com.cssl.controller;

import com.cssl.util.FileUpload;
import com.cssl.util.InfoCode;
import com.cssl.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 * 通用接口
 */
@RestController
@RequestMapping("/common")
@Api("通用接口")
public class CommonController {
    @Resource
   private FileUpload fileUpload;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation(value = "阿里oss文件上传", notes = "阿里oss文件上传")
    public Result upload(@RequestParam("file") MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileURL = fileUpload.upload(file.getBytes(), UUID.randomUUID().toString() + extension);
            return Result.result(InfoCode.OK,InfoCode.MESSAGE_OK,fileURL,System.currentTimeMillis());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
